package com.clouddot.baipai.presenter;


import com.clouddot.baipai.contract.IPersonFragmentContract;
import com.clouddot.baipai.http.entity.response.applyLeaveResponse;
import com.clouddot.baipai.http.entity.response.getLeaveRecordResponse;
import com.clouddot.baipai.http.entity.response.getLeaveTypeResponse;
import com.clouddot.baipai.http.entity.response.getPersonInfoResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.wxy.sdk.base.impl.BasePresenterImpl;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class PersonFragmentPresenter extends BasePresenterImpl<IPersonFragmentContract.IView, IPersonFragmentContract.IModel> implements IPersonFragmentContract.IPresenter {
    public PersonFragmentPresenter(IPersonFragmentContract.IView view, IPersonFragmentContract.IModel model) {
        super(view, model);
    }


    @Override
    public void getPersonInfo() {
        Subscription subscription = mModel.getPersonInfo("GetLoginInfo")
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getPersonInfoResponse>() {
                               @Override
                               public void call(getPersonInfoResponse response) {
                                   if (response.StudentId != null) {
                                       Env.getAppContext().StudentID = response.StudentId;
                                       mView.success(1, response);
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(1, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void applyLeave(String LeaveType, String LeaveTypeText, String LeaveStart, String LeaveEnd, String LeaveReason) {
        Subscription subscription = mModel.applyLeave("LeaveAdd", LeaveType, LeaveTypeText, LeaveStart, LeaveEnd, LeaveReason)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<applyLeaveResponse>() {
                               @Override
                               public void call(applyLeaveResponse response) {
                                   if (response.res.equals("success")) {
                                       mView.success(2, response);
                                   } else {
                                       mView.fail(2, "请假失败");

                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(2, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getLeaveRecord(int pageindex, int pagesize) {
        Subscription subscription = mModel.getLeaveRecord("QueryStudentLeavePage", pageindex, pagesize)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getLeaveRecordResponse>() {
                               @Override
                               public void call(getLeaveRecordResponse response) {
                                   mView.success(3, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(3, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getLeaveType(String id) {
        Subscription subscription = mModel.getLeaveType("LeaveDetail", id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getLeaveTypeResponse>() {
                               @Override
                               public void call(getLeaveTypeResponse response) {
                                   if (!response.LeaveTypeList.isEmpty()) {
                                       mView.success(4, response);
                                   } else {
                                       mView.fail(4, "请假类型为空，请联系管理员去后台设置");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(4, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void pushLeaveNo(String cardID, String LeaveTypeText, String LeaveStart, String LeaveEnd, String LeaveReason) {
        Subscription subscription = mModel.pushLeaveNo(cardID, LeaveTypeText, LeaveStart, LeaveEnd, LeaveReason)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                               @Override
                               public void call(String result) {
                                   if (!result.isEmpty()) {
                                       JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                                       if (jsonObject.get("result").getAsString().equals("0")) {
                                           mView.success(5, "");

                                       } else {
                                           mView.fail(5, "");

                                       }
                                   } else {
                                       mView.fail(5, "");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(5, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }
}
